#pragma  once
#include <iostream>
#include <sys/ipc.h>//shm
#include <sys/shm.h>
#include <cstring>
#include <cerrno>
#include <sys/types.h>//管道
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <cstdlib>

using namespace std;

#include "log.hpp"
#define Path "/home/xuwenhao/"
#define Proj_id 0x55
#define FIFO_FILE "./myfifo"
#define MODE 0664 

Log log;

key_t getkey()
{
    key_t k=ftok(Path,Proj_id);
    if(k<0)
    {
        log("fatal","ftok error %s",strerror(errno));
        return 0;
    }
    log("Info","ftok success k=ox%x",k);
    
    return k;
}

int getshmid(int shmflag)
{
    key_t k=getkey();
    int shmid=shmget(k,SIZE,shmflag);
    if(shmid<0)
    {
        log("fatal","shmget error %s",strerror(errno));
        return 0;
    }

     log("Info","shmid success k=ox%x",shmid);
     return shmid;
}


int Getshm()
{
    return getshmid(IPC_CREAT|0664); //IPC_CREAT
}


class Init
{
public:
  Init()
  {
     int n=mkfifo(FIFO_FILE,MODE);
     if(n==-1)
     {
        perror("open fifo");
        exit(FIFO_CREAT_ERR);
     }
  }

  ~Init()
  {
    int m=unlink(FIFO_FILE);
    if(m==-1)
    {
        perror("delete fifo");
        exit(FIFO_DELETE_ERR);
    }
  }
};

